$(function(){
	// 开始时间
    $("#starttime").datetimepicker({
        format: "YYYY-MM-DD",
        locale: moment.locale('zh-cn'),
        showTodayButton:true,
        showClear:true
    });
    
    // 结束时间
    $("#endtime").datetimepicker({
        format: "YYYY-MM-DD",
        locale: moment.locale('zh-cn'),
        showTodayButton:true,
        showClear:true
    });
    
    // 收入、支出切换
    $("input[name='typeCheckbox']").bootstrapSwitch({
    	onText:$.i18n.prop("income"),
    	offText:$.i18n.prop("cost"),
    	handleWidth:120
    });
    
	// 触发时间选择事件
    $(".glyphicon-calendar").parent().click(function(){
    	$(this).prev("input").focus();
    });
    
    // 加载表格
    var recordTable = new MyTableUtil();
    loadDefaultTable(recordTable);
    
    // 查询按钮绑定事件
    $("#queryBtn").click(function(){
    	var dataParam = {};
    	$("input[mkey]").each(function(){
    		dataParam[$(this).attr("mkey")] = $(this).val();
    	});
    	// 收支类型
    	//dataParam["type"] = $("input[name='typeCheckbox']").bootstrapSwitch('state') ? 1 : 0;
    	
    	recordTable.reload(dataParam);
    });
    
    // 收入记一笔
	$("#incomeBtn").click(function(){
		window.location.href = "record.html?type=" + Constant.accountType.income;
	});
	
	// 支出记一笔
	$("#costBtn").click(function(){
		window.location.href = "record.html?type=" + Constant.accountType.cost;
	});
});

function loadDefaultTable(recordTable){
	recordTable.init({
		id:"biTable",
		url: "/account/record/query",
		type: "mobile",
		isLoading: false,
		callback: function(row){
			var rowClass = "";
			if(row.type == Constant.accountType.cost){
				rowClass = "danger";
			}
			
			row.type = $.i18n.prop(Constant.accountTypeMap[row.type]);
			row.operation = '<a maction="edit">'+ $.i18n.prop("edit") +'</a>&nbsp;|&nbsp;<a maction="del">'+ $.i18n.prop("delete") +'</a>';
			
			return {mid:row.id, mclass:rowClass}
		},
		maction:{
			edit: function(mid, type, event){
				
			},
			del: function(mid, event){
				// 删除确认
				$.modalTips({
					context: $.i18n.prop("are_you_sure_delete"),
					buttons:[{text:$.i18n.prop("confirm"), callfunc:function(){
						$.myajax({
							data: {id: mid},
							url: "/account/record/del",
							callback: function(response){
								if(parseResult(response)){
									$.modalTips({type:"success"});
									// 重新刷新表格
									recordTable.refresh();
								}
							}
						});
					}}]
				});
			}
		}
	});
	recordTable.load({});
}